package com.wandian.weather.mvvm.mapweather;
import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.mylib.libcore.mvvm.BaseViewModel;
import com.mylib.libcore.utils.JsonUtil;
import com.mylib.libcore.utils.RetrofitUtil;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.weather.bean.WeatherInformationBean;
import com.wandian.weather.data.BaseAppApi;
import com.wandian.weather.tools.JmTools;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * @author Administrator
 * @date 2019/12/18-16:11
 * @description MapWeatherViewModel
 **/
public class MapWeatherViewModel extends BaseViewModel {

    public MutableLiveData<WeatherInformationBean> weatherInformationBean = new MutableLiveData<>();
    private Retrofit retrofit;
    private BaseAppApi request;
    public MapWeatherViewModel(@NonNull Application application) {
        super(application);
    }
    //国内天气
    public void getWeatherData(String lat ,String lng){

        //创建Retrofit对象
        retrofit = RetrofitUtil.createWeatherRetrofit();
        //创建网络请求接口实例
        request = retrofit.create(BaseAppApi.class);
        HashMap<String, String> hashMap = new HashMap<>();
        //0：不需要  1：需要
        hashMap.put("weather30", "0");
        hashMap.put("hour24", "0");
        hashMap.put("lifeindex", "0");
        hashMap.put("alarmnews", "1");
        hashMap.put("outline", "1");
        hashMap.put("wind", "1");
        hashMap.put("air", "1");
        hashMap.put("alarmnews", "1");
        hashMap.put("nl", "0");//农历信息
        hashMap.put("need_city", "1");
        hashMap.put("precipitation_2h", "0");
        //--------------------------------
        hashMap.put("lat", lat + "");
        hashMap.put("lng", lng + "");
        //公用参数
        hashMap.put("os","1");
        hashMap.put("pid","4");
        hashMap.put("version","6.5.4");


        Call<Object> call = request.getWeatherData(JmTools.encryptParams(hashMap, "Tianqi.Index"));
        //发送网络请求(异步)
        call.enqueue(new Callback<Object>() {
            @Override
            public void onResponse(Call<Object> call, Response<Object> response) {

                if(UIUtils.isEmpty(response.body()))return;
                Log.d("getWeatherData", "getWeatherData="+ JsonUtil.toJson(response.body()));
                try {
                    JSONObject jsonObject = new JSONObject(JsonUtil.toJson(response.body()));
                    WeatherInformationBean informationBean =(WeatherInformationBean) JsonUtil.parseJsonToBean(jsonObject.optString("data"), WeatherInformationBean.class);
                    if(!UIUtils.isEmpty(informationBean)){
                        weatherInformationBean.setValue(informationBean);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }

            @Override
            public void onFailure(Call<Object> call, Throwable t) {
                Log.d("getWeatherData", "error="+t.getMessage());
            }
        });
    }
}
